﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="r_comercial_evolucao_atendimentos.aspx.cs" Inherits="relatorios_r_comercial_evolucao_atendimentos" %>

<%
    string Permissao_RelatoriosGerais = "" + this.Session["Permissao_RelatoriosGerais"];
    if (Permissao_RelatoriosGerais != "1")
    {
        this.Response.Redirect("login.aspx");
    } 
%>

<div style="background-color: White;">
    <%
        bool hasResults = false;
        DateTime now = DateTime.Now.AddYears(-1);
        DateTime now2 = DateTime.Now;
        
        // FIRST DATE
        string month = this.Request["month"];
        string year = this.Request["year"];            
        int iMonth = -1;
        int iYear = -1;            
        int maxDay = 31;
            
        // Month
        if (string.IsNullOrEmpty(month) == true)
        {
            iMonth = now.Month;
        }
        else
        {
            try
            {
                iMonth = int.Parse(month);
            }
            catch { iMonth = now.Month; }
        }
        // Year            
        if (string.IsNullOrEmpty(year) == true)
        {
            iYear = now.Year;
        }
        else
        {
            try
            {
                iYear = int.Parse(year);
            }
            catch { iYear = now.Year; }                
        }
        maxDay = DateTime.DaysInMonth(iYear, iMonth);

        // SECOND DATE
        string month2 = this.Request["month2"];
        string year2 = this.Request["year2"];
        int iMonth2 = -1;
        int iYear2 = -1;
        int maxDay2 = 31;

        // Month
        if (string.IsNullOrEmpty(month2) == true)
        {
            iMonth2 = now2.Month;
        }
        else
        {
            try
            {
                iMonth2 = int.Parse(month2);
            }
            catch { iMonth2 = now2.Month; }
        }
        // Year            
        if (string.IsNullOrEmpty(year2) == true)
        {
            iYear2 = now2.Year;
        }
        else
        {
            try
            {
                iYear2 = int.Parse(year2);
            }
            catch { iYear2 = now2.Year; }
        }
        maxDay2 = DateTime.DaysInMonth(iYear2, iMonth2);

        string message = "";                     
    %>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>

    <center>
        <form>
            <table>
                <tr>
                    <td>Mês</td>
                    <td>Ano</td>
                    <td></td>
                </tr>

                <tr>
                    <td>
                        <input type="text" id="month" name="month" value="<%= iMonth %>" />
                        <br />
                        <br />
                        <input type="text" id="month2" name="month2" value="<%= iMonth2 %>" />
                    </td>
                    <td>
                        <input type="text" id="year" name="year" value="<%= iYear %>" />
                        <br />
                        <br />
                        <input type="text" id="year2" name="year2" value="<%= iYear2 %>" />
                    </td>
                    <td>
                        <br />
                        <br />
                        <input type="submit" value="ok" />
                    </td>
                </tr>
            </table>
        </form>
    </center>
            
    
    <script type="text/javascript">
        google.load("visualization", "1", { packages: ["corechart"] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = google.visualization.arrayToDataTable([
                ['Data', 'Atendimentos'],
                <%
                    hasResults = false;
                    PL.DataBase.DAO.Instance.Setup(PL.DataBase.DatabaseTypes.SqlServer, PL.General.Instance.BIConnectionString);
                    PL.DataBase.DAO.Instance.Connector.Open();
                        
                    try
                    {   
                        DateTime targetDate = new DateTime(iYear, iMonth, 1);   
                        DateTime maxDate = new DateTime(iYear, iMonth, maxDay);
                        DateTime targetDate2 = new DateTime(iYear2, iMonth2, 1);   
                        DateTime maxDate2 = new DateTime(iYear2, iMonth2, maxDay2);
                                                                       
                        string sql = "";


                        sql += " SELECT        CAST(YEAR(Data) AS VARCHAR(4)) + '-' + RIGHT('00' + CONVERT(NVARCHAR(2), CAST(MONTH(Data) AS VARCHAR(2))), 2) AS DataFinal, COUNT(*) AS Total ";
                        sql += " FROM            BI_Atendimentos_Comerciais ";
                        sql += " WHERE        (Data >= CONVERT(VARCHAR, '" + targetDate.ToString().Split(' ')[0] + "', 103)) AND  ";
                        //sql += "                          (Situacao = 0 OR Situacao = 1) AND (CodNivel = 3 OR CodNivel = 5) AND ";
                        sql += "                          (Data <= CONVERT(VARCHAR, '" + maxDate2.ToString().Split(' ')[0] + "', 103)) ";
                        sql += " GROUP BY CAST(YEAR(Data) AS VARCHAR(4)) + '-' + RIGHT('00' + CONVERT(NVARCHAR(2), CAST(MONTH(Data) AS VARCHAR(2))), 2) ";
                        sql += " ORDER BY DataFinal ";
                        //this.Response.Write(sql);
                        var reader = PL.DataBase.DAO.Instance.Connector.QueryWithReader(sql);                                                
                        while (reader.Read() == true)
                        {                              
                            string solution = "" + reader["DataFinal"];
                            string total = "" + reader["Total"];
                            if (string.IsNullOrEmpty(solution) == false && total != "0,00")
                            {
                                hasResults = true;
                                this.Response.Write("['" + solution + "', " + total.Replace(",", ".") + "],");
                            }
                        }
                        reader.Close();
                    }
                    catch (Exception e)
                    {
                        message = e.Message;
                    }

                    PL.DataBase.DAO.Instance.Connector.Close();
                    this.Response.Write(message);
                %>
            ]);

            <%
                if (hasResults == true)
                {
            %>
                    var options = {
                        title: 'Evolução dos atendimentos do comercial'
                    };

                    var chart = new google.visualization.LineChart(document.getElementById('r_comercial_evolucao_atendimentos'));

                    chart.draw(data, options);
            <%
                }
            %>
        }
    </script>
    <div id="r_comercial_evolucao_atendimentos" style="width: 80%; height: 200px;">
        <center>Evolução dos atendimentos pelo comercial: nenhum dado encontrado</center>
    </div>
</div>
